package prassein.rsm.service.base;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.hibernate.criterion.Criterion;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import prassein.rsm.dao.base.UserDao;
import prassein.rsm.entity.base.User;

@Service
public class UserManager {
	
	@Autowired
	private UserDao userDao;
	
	public UserDao getUserDao() {
		return userDao;
	}

	public void setUserDao(UserDao userDao) {
		this.userDao = userDao;
	}
	
	public void save(User user) {
		userDao.save(user);
	}

	/**
	 * 根据条件查询
	 */
	public User getUser(String name){
		List<Criterion> criterions = new ArrayList<Criterion>();
		if(StringUtils.isNotBlank(name)) {
			criterions.add(Restrictions.eq("userName", name));
		}
		List<User> list = userDao.query(criterions.toArray(new Criterion[criterions.size()]));
		if (list != null && list.size() > 0) {
			return list.get(0);
		} else {
			return new User();
		}
	}
	
	public User getUser(Serializable id){
		return userDao.get(id);
	}
	
	public List<User> getAllUser(){
		return userDao.getAll();
	}
}
